/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.nbsaas.sample.entity.user;


import com.nbsaas.boot.code.annotation.FieldConvert;
import com.nbsaas.boot.code.annotation.FieldName;
import com.nbsaas.boot.code.annotation.FormField;
import com.nbsaas.boot.code.annotation.InputType;
import com.nbsaas.boot.rest.enums.DataScope;
import com.nbsaas.boot.rest.enums.State;
import com.nbsaas.boot.rest.enums.StoreState;
import com.nbsaas.sample.entity.common.AbstractEntity;
import lombok.Data;
import org.hibernate.annotations.Comment;

import javax.persistence.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 用户
 *
 * @author ada
 */


@Data
@Entity
@Table(name = "user_info")
public class UserInfo extends AbstractEntity {

    /**
     * 属性
     */
    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(name = "user_info_attribute", joinColumns = {@JoinColumn(name = "user_id")})
    @MapKeyColumn(name = "name", length = 36)
    @Column(name = "attr", length = 100)
    private Map<String, String> attributes = new HashMap<String, String>();

    /**
     * 用户头像
     */
    @Comment("用户头像")
    private String avatar;

    /**
     * 用户类型
     */
    @Comment("用户类型")
    private Integer catalog;


    /**
     * 手机号码
     */
    @Comment("手机号码")
    @Column(length = 15)
    private String phone;

    /**
     * 用户登录次数
     */
    @Comment("用户登录次数")
    private Integer loginSize = 0;

    /**
     * 用户真实姓名
     */
    @Comment("用户真实姓名")
    @Column(length = 20)
    private String name;

    @Comment("所在组织")
    @FieldName
    @FieldConvert(classType = "Integer")
    @ManyToOne(fetch = FetchType.LAZY)
    private Structure structure;

    @Comment("数据范围")
    @FormField(title = "数据范围", sortNum = "1", type = InputType.el_radio_group, grid = true)
    private DataScope dataScope;


    @Comment("个人介绍")
    @Column(length = 100)
    private String note;

    /**
     * 用户状态
     */
    @Comment("用户状态")
    @Enumerated()
    private State state;

    private StoreState storeState;
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "user_role_links", joinColumns = {@JoinColumn(name = "user_id")})
    private Set<UserRole> roles = new HashSet<UserRole>();

    public static UserInfo fromId(Long id) {
        UserInfo result = new UserInfo();
        result.setId(id);
        return result;
    }


}
